//
//  ___FILENAME___
//  ___PROJECTNAME___
//
//  Created by ___FULLUSERNAME___ on ___DATE___.
//___COPYRIGHT___
//

#import "___FILEBASENAMEASIDENTIFIER___.h"

#define kServerCacheCurrentServer  @"kServerCacheCurrentServer"

@implementation ___VARIABLE_classPrefix:identifier___Server

@end

@interface ___FILEBASENAMEASIDENTIFIER___ ()

@property (strong, nonatomic) NSArray *servers;

@end

@implementation ___FILEBASENAMEASIDENTIFIER___

+ (instancetype)sharedInstance
{
    static id instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = self.new;
    });
    return instance;
}

- (instancetype)init
{
    if (self = [super init]) {
        [self loadServerList];
    }
    return self;
}

- (BOOL)isProduction
{
    return [self.server.name isEqualToString:@"Production"];
}

- (void)loadServerList
{
    NSDictionary *map = [NSDictionary dictionaryWithContentsOfURL:[[NSBundle mainBundle] URLForResource:@"Server" withExtension:@"plist"]];
    NSString *key = [[RFCache sharedCache] objectForKey:kServerCacheCurrentServer];
    if (key == nil) {
        key = map[@"defaultServer"];
    }
    NSArray *list = map[@"servers"];
    self.servers = [RFJSONUtil objectWithJSONValue:list class:[___VARIABLE_classPrefix:identifier___Server class]];
    [self.servers enumerateObjectsUsingBlock:^(___VARIABLE_classPrefix:identifier___Server *obj, NSUInteger idx, BOOL *stop) {
        if ([key compare:obj.name options:NSCaseInsensitiveSearch] == NSOrderedSame) {
            self.server = obj;
            *stop = YES;
        }
    }];
}

@end
